﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.Security.Permission;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace WinformTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        [PermissionInfo(ActionName = @"GetStr")]
        [Checked]
        public void button1_Click(object sender, EventArgs e)
        {
            var bll = PolicyInjection.Create<BLLClass>();

            //var t = new BLLClass();

            MessageBox.Show(bll.GetStr());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }
    }

    public class BLLClass:MarshalByRefObject
    {
        [PermissionInfo(ActionName = @"GetStr")]
        [Checked]
        public string GetStr()
        {
            return "sdfsdff";
        }


    }

    public class Checked:PermissionCheckAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new PermissionCallHandlerCustom()
                       {
                           BeginInvoke = this.OnBeginInvoke,
                           EndInvoke = this.OnEndInvoke,
                           PermissionChecked = this.OnPermissionChecked,
                       };
        }
    }

    public class PermissionCallHandlerCustom:PermissionCallHandler
    {
        public PermissionCallHandlerCustom()
        {
            PermissionCheckedEvent = aaa;
        }

        public static void aaa(object sender, PermissionCheckedEventArgs e)
        {
            if (e.HasError)
            {
                var t = new loginForm();

                t.ShowDialog();

                if (e.CheckTimes < 3)
                {
                    e.IsRetryCheck = true;
                }
            }
        }
    }
}
